package com.yunxi.service.localService.impl.otd;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.model.otd.OtdProduceCapacity;
import com.yunxi.otd.pojo.OtdRuleBaseVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.yunxi.core.service.ServiceImplPlus;
import com.yunxi.dao.otd.OtdRuleBaseMapper;
import com.yunxi.model.otd.OtdRuleBase;
import com.yunxi.service.localService.otd.OtdRuleBaseService;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 生产规则库 服务实现类
 * </p>
 *
 * @author XUQI
 * @since 2019-10-09
 */
@Service
public class OtdRuleBaseServiceImpl extends ServiceImplPlus<OtdRuleBaseMapper, OtdRuleBase> implements
    OtdRuleBaseService {

    @Override
    public ApiResult saveOrUpdate(OtdRuleBaseVO otdRuleBaseVO) {
        // 验证重复 规则+工厂
        LambdaQueryWrapper<OtdRuleBase> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OtdRuleBase::getRule, otdRuleBaseVO.getRule())
            .eq(OtdRuleBase::getWorks, otdRuleBaseVO.getWorks());
        // 如果操作为修改，排除当前行
        if(!StringUtils.isEmpty(otdRuleBaseVO.getId())){
            lambdaQueryWrapper.ne(OtdRuleBase::getId, otdRuleBaseVO.getId());
        }
        int count = count(lambdaQueryWrapper);
        if (count > 0) {
            return ResponseUtil.fail(-1, "规则已存在，规则：" + otdRuleBaseVO.getRule() + " 工厂： " + otdRuleBaseVO.getWorks());
        }
        OtdRuleBase otdRuleBase = new OtdRuleBase();
        BeanUtils.copyProperties(otdRuleBaseVO, otdRuleBase);
        saveOrUpdate(otdRuleBase);
        return ResponseUtil.ok();
    }
}
